package com.smart.campus.school.service.impl;

import java.util.*;

import com.smart.campus.common.utils.DateUtils;
import com.smart.campus.school.domain.ExamAnswer;
import com.smart.campus.school.domain.ExamPaperRelation;
import com.smart.campus.school.domain.ExamQuestion;
import com.smart.campus.school.mapper.ExamAnswerMapper;
import com.smart.campus.school.mapper.ExamPaperRelationMapper;
import com.smart.campus.school.mapper.ExamQuestionMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.smart.campus.school.mapper.ExamPaperMapper;
import com.smart.campus.school.domain.ExamPaper;
import com.smart.campus.school.service.IExamPaperService;
import com.smart.campus.common.core.text.Convert;

/**
 * 试卷Service业务层处理
 * 
 * @author Zhihe.Li
 * @date 2020-05-20
 */
@Service
public class ExamPaperServiceImpl implements IExamPaperService 
{
    @Autowired
    private ExamPaperMapper examPaperMapper;

    @Autowired
    private ExamPaperRelationMapper examPaperRelationMapper;

    @Autowired
    private ExamAnswerMapper examAnswerMapper;

    @Autowired
    private ExamQuestionMapper examQuestionMapper;

    /**
     * 查询试卷
     * 
     * @param id 试卷ID
     * @return 试卷
     */
    @Override
    public ExamPaper selectExamPaperById(Long id)
    {
        ExamPaper examPaper = examPaperMapper.selectExamPaperById(id);
        List<String> questionIdList = examPaperRelationMapper.selectQuestionIdListByPaperId(id);
        String questionIds = String.join(",", questionIdList);
        examPaper.setQuestionIds(questionIds);
        List<ExamQuestion> questionLis = new ArrayList<>();
        for (String questionId : questionIdList) {
            ExamQuestion question = examQuestionMapper.selectExamQuestionById(Long.valueOf(questionId));
            questionLis.add(question);
            ExamAnswer examAnswer = new ExamAnswer();
            examAnswer.setQuestionId(question.getId());
            List<ExamAnswer> answerList = examAnswerMapper.selectExamAnswerList(examAnswer);
            question.setAnswerList(answerList);
        }
        examPaper.setQuestionList(questionLis);
        return examPaper;
    }

    /**
     * 查询试卷列表
     * 
     * @param examPaper 试卷
     * @return 试卷
     */
    @Override
    public List<ExamPaper> selectExamPaperList(ExamPaper examPaper)
    {
        return examPaperMapper.selectExamPaperList(examPaper);
    }

    /**
     * 新增试卷
     * 
     * @param examPaper 试卷
     * @return 结果
     */
    @Override
    public int insertExamPaper(ExamPaper examPaper)
    {

        examPaper.setCreateTime(DateUtils.getNowDate());
        examPaper.setStatus(1);
        int result = examPaperMapper.insertExamPaper(examPaper);
        List<ExamQuestion> questionList = examPaper.getQuestionList();
        if(CollectionUtils.isNotEmpty(questionList)){
            for (ExamQuestion question : questionList) {
                examPaperRelationMapper.insertExamPaperRelation(new ExamPaperRelation(question.getId(),examPaper.getId()));
            }
        }
        return result;
    }

    /**
     * 修改试卷
     * 
     * @param examPaper 试卷
     * @return 结果
     */
    @Override
    public int updateExamPaper(ExamPaper examPaper)
    {
        examPaper.setUpdateTime(DateUtils.getNowDate());
        List<String> old = examPaperRelationMapper.selectQuestionIdListByPaperId(examPaper.getId());
        if(CollectionUtils.isNotEmpty(old)){
                examPaperRelationMapper.deleteExamPaperRelationByIds(String.join(",",old).split(","));
        }
        List<ExamQuestion> questionList = examPaper.getQuestionList();
        if(CollectionUtils.isNotEmpty(questionList)){
            for (ExamQuestion question : questionList) {
                examPaperRelationMapper.insertExamPaperRelation(new ExamPaperRelation(question.getId(),examPaper.getId()));
            }
        }
        return examPaperMapper.updateExamPaper(examPaper);
    }

    /**
     * 删除试卷对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteExamPaperByIds(String ids)
    {
        return examPaperMapper.deleteExamPaperByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除试卷信息
     * 
     * @param id 试卷ID
     * @return 结果
     */
    @Override
    public int deleteExamPaperById(Long id)
    {
        return examPaperMapper.deleteExamPaperById(id);
    }

    /**
     * 更新试卷题目信息
     *
     * @param questionId
     * @param type       1 添加 2删除
     */
    @Override
    public int updatePaperQuestion(Long questionId,Long paperId ,Integer type) {
        if(type == 1){
            ExamQuestion examQuestion = examQuestionMapper.selectExamQuestionById(questionId);
            ExamPaper oldPaper = examPaperMapper.selectExamPaperById(paperId);
            ExamPaper examPaper = new ExamPaper();
            examPaper.setTotalScore(oldPaper.getTotalScore()+examQuestion.getScore());
            examPaper.setId(paperId);
            examPaperMapper.updateExamPaper(examPaper);
            return examPaperRelationMapper.insertExamPaperRelation(new ExamPaperRelation(questionId,paperId));
        }else {
            ExamQuestion examQuestion = examQuestionMapper.selectExamQuestionById(questionId);
            ExamPaper oldPaper = examPaperMapper.selectExamPaperById(paperId);
            ExamPaper examPaper = new ExamPaper();
            examPaper.setTotalScore(oldPaper.getTotalScore()-examQuestion.getScore());
            examPaper.setId(paperId);
            examPaperMapper.updateExamPaper(examPaper);
            return examPaperRelationMapper.deleteExamPaperRelationByPaperIdAndQuestionId(new ExamPaperRelation(questionId,paperId));
        }
    }

    /**
     * 获取试卷答案
     *
     * @param id
     * @return
     */
    @Override
    public Map<Integer,Map<Long, List<ExamAnswer>>> getPaperAnswerByPaperId(Long id) {
        ExamPaper paper = examPaperMapper.selectExamPaperById(id);

        List<String> questionIdList = examPaperRelationMapper.selectQuestionIdListByPaperId(id);
        Map<Long, List<ExamAnswer>> map = new LinkedHashMap<>();
        List<Long> idList = new ArrayList<>();
        for (String questionId : questionIdList) {

            idList.add(Long.valueOf(questionId));
        }
        List<ExamQuestion> questionList = examQuestionMapper.selectExamQuestionsByIds(idList);
        Map<Integer,Map<Long, List<ExamAnswer>>> typeMap = new LinkedHashMap<>();

        for (ExamQuestion question : questionList) {
            ExamAnswer examAnswer = new ExamAnswer();
            examAnswer.setQuestionId(Long.valueOf(question.getId()));
            List<ExamAnswer> answerList = examAnswerMapper.selectExamAnswerList(examAnswer);
            //按试题类型分组
            if(typeMap.containsKey(question.getType())){
                Map<Long, List<ExamAnswer>> resMap = typeMap.get(question.getType());
                resMap.put(Long.valueOf(question.getId()),answerList);
            }else {
                Map<Long, List<ExamAnswer>> resMap = new LinkedHashMap<>();
                resMap.put(Long.valueOf(question.getId()),answerList);
                typeMap.put(question.getType(),resMap);
            }

        }
        return typeMap;
    }
}
